/*
 * SQLManager.java
 *
 * Created on November 5, 2007, 11:31 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package conector;

import java.sql.*;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
/**
 *
 * @authorMario Batres
 */
public class SQLManager {
    private static final String user = "root";
    private static final String url = "jdbc:mysql://localhost/mibase";
    private static final String password = "";
    
    public SQLManager(){
    
    }
      
    
    protected Connection getConexion(){
        try{    
            try{
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            }catch(Exception e){
                return null;
            } 
            Connection conexion = DriverManager.getConnection(url, user, password);            
            return conexion;
        } catch (SQLException ex) {           
            //JOptionPane.showMessageDialog(null, "SQLException: " + ex.getMessage());	
        }     
            
        return null;
    }
    
    public boolean isAdministrador(String login, String passwd){        
         try{                
            Connection conexion = this.getConexion();
            
            if(conexion != null){
                Statement s = conexion.createStatement();
                String query = "select * from Administrador where login='" + login + "' and passwd='" + passwd+ "';";
                ResultSet rs = s.executeQuery(query);                        
                boolean res = rs.next();                
                conexion.close();      
                return res;            
            }
            
        } catch (SQLException ex) {            
            //JOptionPane.showMessageDialog(null, "SQLException: " + ex.getMessage());	
        }     
        return false;
    }
    
    private String getQueryDML(int opcion, String cuenta,  String usuario){
        String query = "";
        /*switch(opcion){
            case Constantes.INSERT:{
                query += "insert into Asignacion values(";
                query += "trim('" + cuenta + "'), ";
                query += "trim('" + usuario + "'));";
                break;
            }
        }*/
        return query;
    }
    /*
     select i1.codigo as cod_moneda, i1.nombre as moneda, i2.codigo as cod_moneda_a, i2.nombre as moneda_a, i.monto from Tipo_Cambio i,
(select codigo, nombre from Moneda where nombre = 'Dolar') i1,
(select codigo, nombre from Moneda where nombre = 'Quetzal') i2
where i1.codigo = i.moneda and i2.codigo = i.moneda_a;
     */
    public void InsertarRegistro(int id, String nombre){
        try{
            Connection conexion = this.getConexion();
            if(conexion != null){
                Statement s = conexion.createStatement();                
                
                String query = "insert into prueba values (";
                query += id + ", ";
                query += "'" + nombre + "');";
                        
                s.executeUpdate(query);
                s.close();
                conexion.close();                
            }
        }catch(SQLException ex){
            JOptionPane.showMessageDialog(null, "SQLException: " + ex.getMessage(), "Insertar Registro", JOptionPane.ERROR_MESSAGE);
        }
    }
}
